<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
      <el-form-item label="校区" prop="shopid" disabled="disabled">
        <el-select v-model="dataForm.shopid" placeholder= "校区" >
          <el-option v-for="(item, index) in shopids"
                     :label="item.shopname"
                     :key="item.shopname"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="课程名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="课程名字" readOnly="true"></el-input>
    </el-form-item>
      <el-form-item label="单节课时" prop="total">
        <el-input v-model="dataForm.classPeriod" placeholder="单节课时"></el-input>
      </el-form-item>
    <el-form-item label="课程总节数" prop="total">
      <el-input v-model="dataForm.total" placeholder="课程总节数" readOnly="true"></el-input>
    </el-form-item>
    <el-form-item label="课程配图" prop="pic">
      <el-input v-model="dataForm.pic" placeholder="课程配图" readOnly="true"></el-input>
    </el-form-item>
    <el-form-item label="支付码" prop="pic2">
      <el-input v-model="dataForm.pic2" placeholder="支付码" readOnly="true"></el-input>
    </el-form-item>
    <!--<el-form-item label="老师编号" prop="teacherid">-->
      <!--<el-input v-model="dataForm.teacherid" placeholder="课程老师编号" readOnly="true"></el-input>-->
    <!--</el-form-item>-->
    <el-form-item label="老师姓名" prop="teachername">
      <el-input v-model="dataForm.teachername" placeholder="老师姓名" readOnly="true"></el-input>
    </el-form-item>
    <el-form-item label="课程内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="课程内容" readOnly="true"></el-input>
    </el-form-item>
    <!--<el-form-item label="课程开始日期" prop="begindate">-->
      <!--<el-input v-model="dataForm.begindate" placeholder="课程开始日期" readOnly="true"></el-input>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="课程结束日期" prop="enddate">-->
      <!--<el-input v-model="dataForm.enddate" placeholder="课程结束日期" readOnly="true"></el-input>-->
    <!--</el-form-item>-->
      <el-form-item label="上课时间" prop="courseweek">
        <div v-for="(list,index) in dataForm.coursetime"  >
        <el-time-picker v-model="list.courseweektime" type="datetime" disabled="disabled" @change="dateChanget" format="HH:mm" value-format="HH:mm" placeholder="选择时间" >
        </el-time-picker>
        <el-select  v-model="list.courseweek" value-key="id" placeholder="选择周几" disabled="disabled">
          <el-option v-for="item in payeeoption"
                     :label="item.courseweekname"
                     :key="item.id"
                     :value="item"
          ></el-option>
        </el-select>
        </div>
      </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注" readOnly="true"></el-input>
    </el-form-item>
    <el-form-item label="新增时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="新增时间" readOnly="true"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updatetime">
      <el-input v-model="dataForm.updatetime" placeholder="修改时间" readOnly="true"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">返回</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          shopid: '',
          name: '',
          total: '',
          pic: '',
          pic2: '',
          teacherid: '',
          teachername: '',
          content: '',
          begindate: '',
          enddate: '',
          remark: '',
          createtime: '',
          updatetime: '',
          coursetime: [{'courseweek': '', 'courseweektime': ''}]
        },
        shopids: '',
        payeeoption: [{'courseweekname': '星期一', 'id': 'Mon'}, {'courseweekname': '星期二', 'id': 'Tue'}, {'courseweekname': '星期三', 'id': 'Wed'}, {'courseweekname': '星期四', 'id': 'Thu'}, {'courseweekname': '星期五', 'id': 'Fri'}, {'courseweekname': '星期六', 'id': 'Sat'}, {'courseweekname': '星期日', 'id': 'Sun'}],
        dataRule: {
          shopid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '课程名字不能为空', trigger: 'blur' }
          ],
          total: [
            { required: true, message: '课程总节数不能为空', trigger: 'blur' }
          ],
          pic: [
            { required: true, message: '课程配图不能为空', trigger: 'blur' }
          ],
          teacherid: [
            { required: true, message: '课程老师不能为空', trigger: 'blur' }
          ],
          teachername: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '课程内容不能为空', trigger: 'blur' }
          ],
          begindate: [
            { required: true, message: '课程开始日期不能为空', trigger: 'blur' }
          ],
          enddate: [
            { required: true, message: '课程结束日期不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '新增时间不能为空', trigger: 'blur' }
          ],
          updatetime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
      this.getShopInfo()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/course/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopid = data.course.shopid
                this.dataForm.name = data.course.name
                this.dataForm.total = data.course.total
                this.dataForm.pic = data.course.pic
                this.dataForm.pic2 = data.course.pic2
                this.dataForm.teacherid = data.course.teacherid
                this.dataForm.teachername = data.course.teachername
                this.dataForm.content = data.course.content
                this.dataForm.begindate = data.course.begindate
                this.dataForm.enddate = data.course.enddate
                this.dataForm.remark = data.course.remark
                this.dataForm.createtime = data.course.createtime
                this.dataForm.updatetime = data.course.updatetime
                this.dataForm.classPeriod = data.course.classPeriod
                this.getOneCourseTime()
                // this.dataForm.coursetime = [{'courseweek': {'courseweekname': '星期一', 'id': 'Mon'}, 'courseweektime': '11:11'}, {'courseweek': {'courseweekname': '星期二', 'id': 'Mon'}, 'courseweektime': '13:11'}, {'courseweek': {'courseweekname': '星期三', 'id': 'Mon'}, 'courseweektime': '14:11'}]
              }
            })
          }
        })
      },
      addDomain () {
        this.dataForm.coursetime.push({'courseweek': '', 'courseweektime': ''})
      },
      del: function (index) {
        this.dataForm.coursetime.splice(index, 1)
      },
      dateChanget (val) {
        this.dataForm.coursetime.courseweektime = val
      },
      // 查询课程的所有课程时间
      getOneCourseTime () {
        let courseid = this.dataForm.id
        this.$http({
          url: this.$http.adornUrl(`/business/course/getCourseTime`),
          method: 'get',
          params: {'id': courseid}
        }).then(({data}) => {
          this.dataForm.coursetime = data.courseTime
        })
      },
      // 获取机构下所有店铺信息
      getShopInfo () {
        let trainId = '1'
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {'trainId': trainId}
        }).then(({data}) => {
          this.shopids = data.list
        })
      },
      qureyCourseInfo () {
        this.$http({
          url: this.$http.adornUrl('/business/student/qureyCourseInfo'),
          method: 'get',
          params: {'courseId': this.dataForm.id}
        }).then(({data}) => {
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/course/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shopid': this.dataForm.shopid,
                'name': this.dataForm.name,
                'total': this.dataForm.total,
                'pic': this.dataForm.pic,
                'pic2': this.dataForm.pic2,
                'teacherid': this.dataForm.teacherid,
                'teachername': this.dataForm.teachername,
                'content': this.dataForm.content,
                'begindate': this.dataForm.begindate,
                'enddate': this.dataForm.enddate,
                'remark': this.dataForm.remark,
                'createtime': this.dataForm.createtime,
                'updatetime': this.dataForm.updatetime,
                'coursetime': this.dataForm.coursetime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
